package com.yjck.ijpay.controller;

import cn.hutool.core.util.IdcardUtil;
import com.xkzhangsan.time.utils.StringUtil;
import com.yjck.ijpay.pojo.BaseResponse;
import com.yjck.ijpay.pojo.XhMember;
import com.yjck.ijpay.utils.WeChatUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lyz
 * @version [1.0, 2021-04-21]
 * @Description: C端用户登录
 */
@Slf4j
@RestController
@RequestMapping("/login")
public class WxLoginController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

//    @Autowired
//    private IXhMemberService xhMemberService;
//    @Autowired
//    private IXhMemberMoneyService xhMemberMoneyService;
//    @Autowired
//    private CommonService commonService;
//    @Autowired
//    private IXhMemberComboService xhMemberComboService;
//    @Autowired
//    private UserLoginManager userLoginManager;

    /**
     * 登陆接口
     */
    @GetMapping
    public BaseResponse login(String code) {
        if (StringUtils.isBlank(code)) {
            return BaseResponse.error("code为空.");
        }
        Map<String, Object> map = new HashMap<>();
        // 获取openid
        String openid = WeChatUtil.login(code);
        if (StringUtil.isEmpty(openid)) {
            return BaseResponse.error("解析code失败");
        }
//        XhMember xhMember = xhMemberService.getOne(new QueryWrapper<XhMember>().lambda().eq(XhMember::getOpenId, openid));
        // 数据库不存在
//        if (StringUtil.isEmpty(xhMember)) {
//            XhMember newXhMember = new XhMember();
//            newXhMember.setOpenId(openid);
//            xhMemberService.save(newXhMember);
//            map.put("id", newXhMember.getId());
//            map.put("status", false);
//            return BaseResponse.success(map);
//        }
//        if (xhMember.getIsEnabled().equals(EnabledEnum.NO.getCode())) {
//            return BaseResponse.error("账号被禁用,请联系管理员.");
//        }
//        if (StringUtil.isEmpty(xhMember.getIdCard())) {
//            map.put("id", xhMember.getId());
//            map.put("status", false);
//        } else {
//            String token = JwtUtil.createJwt(String.valueOf(xhMember.getId()), xhMember.getNickname(), JwtConstants.MINI);
//            userLoginManager.saveMini(xhMember.getId(), token);
//            map.put("token", token);
//            map.put("status", true);
//        }
        return BaseResponse.success(map);
    }

    /**
     * <pre>
     * 获取用户绑定手机号信息
     * </pre>
     */
    @GetMapping("/phone")
    public BaseResponse phone(String code, String encryptedData, String iv) {
        String purePhoneNumber = WeChatUtil.phone(code, encryptedData, iv);
        if (StringUtil.isEmpty(purePhoneNumber)) {
            return BaseResponse.error("获取用户手机号失败");
        }
        return BaseResponse.success(purePhoneNumber);

    }

    /**
     * <pre>
     * 获取用户信息接口
     * </pre>
     */
    @PostMapping("/info")
    public BaseResponse info(@RequestBody XhMember xhMember) {
//        String xhMemberNo = commonService.createNo(NoPrefixEnum.MEMBER);
//        xhMember.setMemberNo(xhMemberNo);
//        boolean valid = IdcardUtil.isValidCard(xhMember.getIdCard());
//        if (valid) {
//            boolean updateById = xhMemberService.updateById(xhMember);
//            if (updateById) {
//                String token = JwtUtil.createJwt(String.valueOf(xhMember.getId()), xhMember.getNickname(), JwtConstants.MINI);
//                userLoginManager.saveMini(xhMember.getId(), token);
//                // 初始化钱包信息
//                XhMemberMoney xhMemberMoney = new XhMemberMoney();
//                xhMemberMoney.setMemberId(xhMember.getId());
//                xhMemberMoneyService.save(xhMemberMoney);
//                // 初始化套餐表
//                XhMemberCombo xhMemberCombo = new XhMemberCombo();
//                xhMemberCombo.setMemberId(xhMember.getId());
//                xhMemberComboService.save(xhMemberCombo);
//                return BaseResponse.success(token);
//            }
//            return BaseResponse.error("添加用户信息失败");
//        }
//        return BaseResponse.error("请填写正确的身份证信息");
//    }
        return BaseResponse.success();
    }
}
